<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>客户资源管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/font.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/lib/layui/layui.js"
            charset="utf-8"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
<div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="">首页</a>
        <a href="">客户信息</a>
        <a>
          <cite>列表</cite></a>
      </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:40px">ဂ</i></a>
</div>
<div class="x-body">
    <div class="layui-row">
        <form class="layui-form layui-col-md12 x-so" onsubmit="return false;">
            <div class="layui-input-inline">
                <select id="condition1K">
                    <option value="customerName">客户姓名</option>
                    <option value="customerTel">联系电话</option>
                </select>
            </div>
            <input class="layui-input" id="condition1V">
            分配状态：
            <div class="layui-input-inline">
                <select id="assignStatus">
                    <option value="">不限</option>
                    <option value="0">未分配</option>
                    <option value="1">已分配</option>
                </select>
            </div>
            <button class="layui-btn" id="search"><i class="layui-icon">&#xe615;</i></button>
        </form>
    </div>
    <xblock class="do-table">
        <button class="layui-btn"
                onclick="x_admin_show('添加客户','${pageContext.request.contextPath}/cm/customer/add/page')"><i
                class="layui-icon"></i>添加
        </button>
    </xblock>
    <table id="data" lay-filter="customer"></table>

</div>
<script>
    var table;
    layui.use(['laydate', 'table'], function () {
        var laydate = layui.laydate;
        table = layui.table;
        //执行一个laydate实例
        laydate.render({
            elem: '#start' //指定元素
        });
        //执行一个laydate实例
        laydate.render({
            elem: '#end' //指定元素
        });

        table.render({
            elem: '#data'
            , height: 600
            , url: '${pageContext.request.contextPath}/cm/customer/list/data' //数据接口
            , method: "post"
            , page: {limit: 2, limits: [2, 5, 10, 20, 30, 40, 50]} //开启分页
            , cols: [[ //表头，列宽总和最多98%，否则出现水平滚动条
                {type: 'checkbox', fixed: 'left', width: '5%'}
                , {type: 'numbers', title: '序号', width: '7%', sort: true}
                , {field: 'name', title: '客户姓名', width: '10%', sort: true}
                , {
                    field: 'gender', title: '性别', width: '8%', sort: true, templet: function (d) {
                        if (d.gender === 0) {
                            return '男'
                        } else if (d.gender === 1) {
                            return '女'
                        } else if (d.gender === 2) {
                            return '保密'
                        }
                    }
                }
                , {
                    field: 'type', title: '类型', width: '10%', sort: true, templet: function (d) {
                        if (d.type === 0) {
                            return '普通客户'
                        } else if (d.type === 1) {
                            return '大客户'
                        } else if (d.type === 2) {
                            return 'VIP客户'
                        } else if (d.type === 3) {
                            return '银卡客户'
                        } else if (d.type === 4) {
                            return '金卡客户'
                        }
                    }
                }
                , {
                    field: 'status', title: '状态', width: '8%', sort: true, templet: function (d) {
                        // 根据状态设置按钮效果
                        if (d.status === 0) {
                            return '已禁用'
                        } else if (d.status === 1) {
                            return '正常'
                        } else if (d.status === 2) {
                            return '已流失'
                        }
                    }
                }
                , {
                    field: 'assignStatus', title: '分配状态', width: '10%', templet: function (d) {
                        if (d.assignStatus === 0) {
                            return '未分配'
                        } else if (d.assignStatus === 1) {
                            return '已分配'
                        }
                    }
                }
                , {field: 'createTime', title: '录入时间', width: '15%', sort: true}
                , {
                    field: 'createEmp', title: '录入人', width: '10%', sort: true, templet: function (d) {
                        return d.createEmp.emplRealName
                    }
                }
                , {fixed: 'right', title: '操作', width: '15%', align: 'center', toolbar: '#do'}
            ]]
        });
        //工具条事件
        table.on('tool(customer)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）

            if (layEvent === 'status') { //改变状态
                // 先改变状态
                data.status = data.status === 0 ? 1 : 0;
                var customerData = {'id': data.id, 'status': data.status};
                // 发送ajax改变状态
                $.ajax({
                    type: "post",
                    url: "${pageContext.request.contextPath}/cm/customer/updateStatus",
                    data: customerData,
                    dataType: "json",
                    success: function (res) {
                        if (res.code === 200) {
                            // 重新加载表格
                            table.reload("data");
                        }
                    }
                });

            } else if (layEvent === 'detail') { //编辑
                // 查看
                x_admin_show('查看客户详情', '${pageContext.request.contextPath}/cm/customer/detail/page?id=' + data.id)
            } else if (layEvent === 'edit') {
                // 编辑
                x_admin_show('修改客户信息', '${pageContext.request.contextPath}/cm/customer/edit/page?id=' + data.id)
            }
        });

        // 搜索按钮
        $("#search").click(function () {
            //获取对应的数据
            // 获取条件一的key
            let condition1K = $("#condition1K").val();
            let condition1V = $("#condition1V").val();
            // 获取条件二
            let customerAssignStatus = $("#assignStatus").val();
            // 组装总共的提交参数
            let queryParam = {};
            Object.defineProperty(queryParam, condition1K, {
                value: condition1V,
                writable: true,
                enumerable: true,
                configurable: true
            });
            queryParam.customerAssignStatus = customerAssignStatus;
            console.log(queryParam);
            // 表格数据的重载
            table.reload("data", {where: queryParam})
        });
    });
</script>

<script type="text/html" id="do">
    {{#  if(d.status === 0){ }}
    <a class="layui-btn layui-btn-xs" lay-event="status">启用</a>
    {{#  } }}
    {{#  if(d.status === 1){ }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="status">禁用</a>
    {{#  } }}
    {{#  if(d.status === 2){ }}
    <a class="layui-btn layui-btn-primary layui-border-black layui-btn-xs">已流失</a>
    {{#  } }}
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="detail">查看</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="edit">修改</a>
</script>

</body>

</html>